package cn.qf.shop.service.impl;


import cn.qf.shop.mapper.MaintainMapper;
import cn.qf.shop.pojo.entity.Maintain;

import cn.qf.shop.pojo.qo.MaintainQO;
import cn.qf.shop.service.MaintainService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class MaintainServiceImpl implements MaintainService {
    @Resource
    MaintainMapper maintainMapper;
    public int addMain(MaintainQO maintainQO) {
        Maintain maintain = new Maintain();
        BeanUtils.copyProperties(maintainQO,maintain);
        int i = maintainMapper.insertMain(maintain);
        return i;
    }

    public int updateMain(MaintainQO maintainQO) {
        Maintain maintain = new Maintain();
        BeanUtils.copyProperties(maintainQO,maintain);
        int i = maintainMapper.updateMain(maintain);
        return i;
    }

    public int deleteMain(int maintainId) {
        int i = maintainMapper.deleteMain(maintainId);
        return i;
    }

    public int deleteAllMain(List<Integer> maintainIds) {
        int i = maintainMapper.deleteAllMain(maintainIds);
        return i;
    }

    public List<Maintain> showMain() {
        List<Maintain> maintains = maintainMapper.showAll();
        return  maintains;

    }

    public List<MaintainQO> MaintainList(String keyword, String createTime, String updateTime, int limit, int offset) {
        List<Maintain> maintains = maintainMapper.selectAll(keyword, createTime, updateTime, (limit - 1) * offset, offset);
        if (maintains.size() == 0) {
            return null;
        }
        List<MaintainQO> maintainQOS =new ArrayList();

        for (Maintain maintain : maintains) {
            MaintainQO maintainQO = new MaintainQO();
            BeanUtils.copyProperties(maintain, maintainQO);
            maintainQOS.add(maintainQO);
        }
        return maintainQOS;
        }


}
